package com.jinyi.wwdeng.ui.user.fragment

import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jinyi.base.network.IRequestCallback
import com.jinyi.base.network.NetworkUtils
import com.jinyi.base.utils.img.ImageUtil
import com.jinyi.lib_baseadapter.BGAOnItemChildClickListener
import com.jinyi.lib_baseadapter.BGARecyclerViewAdapter
import com.jinyi.lib_baseadapter.BGAViewHolderHelper
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.FragmentAccountBinding
import com.jinyi.wwdeng.network.ApiService
import com.jinyi.wwdeng.network.HttpBaseParams.addCommonParams
import com.jinyi.wwdeng.pro.ProFragment
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/24 20:03
 * @Annotation：我的账户
 */
class AccountFragment: ProFragment() {

    lateinit var binding: FragmentAccountBinding
    private var mItemAdapter: ItemAdapter? = null
    override fun getRootView(inflater: LayoutInflater): View {
        binding = FragmentAccountBinding.inflate(inflater)
        return binding.root
    }

    override fun initView() {
        initRefreshLayout()
        initRecyclerView()
    }

    override fun initListener() {
    }

    override fun initData() {
        showLoading("")
        loadAccountAction()
    }

    /* 初始化刷新 */
    private fun initRefreshLayout() {
        //禁止加载
        binding.refreshLayout.setEnableLoadMore(false)
        binding.refreshLayout.setEnableAutoLoadMore(false)
        binding.refreshLayout.setOnRefreshListener {
            loadAccountAction()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            binding.refreshLayout.finishLoadMore()
        }
    }

    /**
     * 初始化账户列表
     */
    private fun initRecyclerView() {
        binding.recyclerView.visibility = View.GONE
        binding.tvTips.visibility = View.GONE
        binding.emptyLayout.emptyLayout.visibility = View.GONE

        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.isNestedScrollingEnabled = false
        binding.recyclerView.isFocusableInTouchMode = false //不需要焦点
        binding.recyclerView.layoutManager = LinearLayoutManager(context)
//        val topDecoration = App.dp2px(12f)
//        binding.recyclerView.addItemDecoration(LinearSpacingItemDecoration(0,0,topDecoration,topDecoration))
        mItemAdapter = ItemAdapter(binding.recyclerView)
        binding.recyclerView.adapter = mItemAdapter!!
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        mItemAdapter!!.setOnItemChildClickListener(BGAOnItemChildClickListener { _, childView, position ->
        })
    }

    /**
     * 设置账户信息
     */
    private fun setAccountList(list: ArrayList<JSONObject>){
        mItemAdapter!!.data = list

        binding.recyclerView.visibility = View.VISIBLE
        binding.tvTips.visibility = View.VISIBLE
        binding.emptyLayout.emptyLayout.visibility = View.GONE
    }

    class ItemAdapter(recyclerView: RecyclerView?) : BGARecyclerViewAdapter<JSONObject>(recyclerView, R.layout.item_account) {
        override fun setItemChildListener(helper: BGAViewHolderHelper, viewType: Int) {
            helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        }
        override fun fillData(helper: BGAViewHolderHelper, position: Int, model: JSONObject) {
            helper.setText(R.id.tvName,model.optString("name"))
            helper.setText(R.id.tvNumber,model.optString("account"))
            val imgLogo = helper.getImageView(R.id.ivLogo)
            ImageUtil.getInstance().loadImage(mContext,imgLogo,model.optString("logo"),R.mipmap.pic_holder_icon)
        }
    }

    /**
     * 加载账户列表
     */
    private fun loadAccountAction(){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.API_COMMON_URL)
        val params = JSONObject().addCommonParams().apply {
            put("path", ApiService.API_PATH.API_PATH_ACCOUNT_LIST)
        }
        NetworkUtils.post(httpUrl,params,object : IRequestCallback {
            //请求成功
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")){
                    0 -> {//成功
                        val array = data.optJSONArray("obj")
                        if (array != null && array.length() > 0) {
                            val accountList = arrayListOf<JSONObject>()
                            for (i in 0..<array.length()){
                                accountList.add(array.optJSONObject(i))
                            }
                            setAccountList(accountList)
                        } else {
                            binding.recyclerView.visibility = View.GONE
                            binding.tvTips.visibility = View.GONE
                            binding.emptyLayout.emptyLayout.visibility = View.VISIBLE
                        }
                    }
                    else -> {//失败
                        showToast(data.optString("msg"))
                    }
                }
            }
            //请求失败
            override fun onRequestError(data: JSONObject) {
                showToast(data.optString("errorMsg"))
            }
            //请求完成
            override fun onRequestFinish() {
                hideLoading()
                binding.refreshLayout.finishRefresh()
            }
        })
    }
}